﻿using System;
using UnityEngine;

// Token: 0x020003DA RID: 986
internal class AudioVolumeAnalyzer
{
	// Token: 0x1700025C RID: 604
	// (get) Token: 0x060018A9 RID: 6313 RVA: 0x000A10CC File Offset: 0x0009F2CC
	public bool HasAudio
	{
		get
		{
			return this.audio != null;
		}
	}

	// Token: 0x060018AA RID: 6314 RVA: 0x000A10DC File Offset: 0x0009F2DC
	public float AnalyzeSound(float checkTime = 0.1f)
	{
		if (this.audio == null || !this.audio.isPlaying)
		{
			return 0f;
		}
		int timeSamples = this.audio.timeSamples;
		if (timeSamples == 0)
		{
			return 0f;
		}
		this.CheckBuffer(checkTime);
		int num = this.audio.clip.samples * this.audio.clip.channels;
		int num2 = num - timeSamples * this.audio.clip.channels;
		if (this.audio.clip.GetData(this.buf, timeSamples))
		{
			float num3 = 0f;
			for (int i = 0; i < this.buf.Length; i++)
			{
				if (!this.audio.loop && i >= num2)
				{
					break;
				}
				num3 += Mathf.Abs(this.buf[i]);
			}
			num3 /= (float)this.buf.Length;
			return Mathf.Abs(num3);
		}
		return 0f;
	}

	// Token: 0x060018AB RID: 6315 RVA: 0x000A11EC File Offset: 0x0009F3EC
	public void SetAudio(AudioSource audio)
	{
		this.audio = audio;
	}

	// Token: 0x060018AC RID: 6316 RVA: 0x000A11F8 File Offset: 0x0009F3F8
	private void CheckBuffer(float checkTime)
	{
		int num = (int)((float)this.audio.clip.frequency * checkTime);
		if (num <= 0)
		{
			num = 1;
		}
		int num2 = this.audio.clip.channels * num;
		if (this.buf == null || this.buf.Length != num2)
		{
			this.CreateBuffer(num2);
		}
	}

	// Token: 0x060018AD RID: 6317 RVA: 0x000A1258 File Offset: 0x0009F458
	private void CreateBuffer(int bufSize)
	{
		this.buf = new float[bufSize];
	}

	// Token: 0x04001C21 RID: 7201
	private AudioSource audio;

	// Token: 0x04001C22 RID: 7202
	private int frequency;

	// Token: 0x04001C23 RID: 7203
	private int channels;

	// Token: 0x04001C24 RID: 7204
	private float[] buf;
}
